<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/18 15:51
 */

namespace module\user\library\validate;

use mof\Validate;

class CaptchaValidate
{
    public static function register(): void
    {
        Validate::maker(function ($validate) {
            $validate->extend('captcha', function ($value, $rule, $data) {
                list($action, $name) = explode(',', $rule);
                //#表开头表示提供的是一个手机号，否则就是$data里的key名
                $mobile = preg_match('/#[0-9]+/', $name)
                    ? substr($name, 1)
                    : ($data[$name] ?? '');
                return static::check($mobile, $value, $action);
            });
        });
    }

    public static function check($mobile, $captcha, $action): bool|string
    {
        if ($captcha) {
            //TODO 验证码校验
            if (!$mobile) return '手机号不能为空~';
            if ($captcha === '123456') return true;
            return '验证码错误~';
        }
        return true;
    }
}